#include "borrowerbookwidget.h"
#include "ui_borrowerbookwidget.h"
#include"QMouseEvent"

borrowerBookwidget::borrowerBookwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::borrowerBookwidget)
{

    ui->setupUi(this);
    this->setLayout(ui->mainLayout);

    //ui->tableWidget->setColumnCount(9);
    //ui->tableWidget->setGeometry(0, 90, 1500, 1100);
    ui->tableWidget->verticalHeader()->hide();
    //设置表格字体
    QFont font("宋体", 12, QFont::Bold);
    ui->tableWidget->setFont(font);
    ui->tableWidget->horizontalHeader()->setFont(font);

    // 设置QTableWidget不可编辑，通过设置编辑触发模式为NoEditTriggers
        ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //设置分类
    QStringList kinds;
    kinds<<"全部"<<"历史"<<"天文"<<"科学"<<"小说"<<"诗歌";
    ui->comboBox->addItems(kinds);
    //初始化
    ui->lineEdit->setFocus();
    init();
    //安装事件过滤器
        ui->tableWidget->installEventFilter(this);

        //搜索提示!!!!!!!!!!!!!!11
        ui->lineEdit->setPlaceholderText("书名/作者/出版社");



}

bool borrowerBookwidget::eventFilter(QObject *watched, QEvent *event)
{
   //qDebug()<<"点击事件";

    if (watched == ui->tableWidget && event->type() == QEvent::MouseButtonPress)
    {

        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        int row = ui->tableWidget->rowAt(mouseEvent->y());
        int col = ui->tableWidget->columnAt(mouseEvent->x());
        qDebug() << "鼠标点击坐标: (" << mouseEvent->x() << ", " << mouseEvent->y() << ")";
        qDebug() << "计算得到的行列索引: (" << row << ", " << col << ")";
        if (col == ui->tableWidget->columnCount() - 1) // 假设最后一列是包含QCheckBox的列
        {
            QWidget *cellWidget = ui->tableWidget->cellWidget(row, col);
            if (cellWidget && cellWidget->inherits("QCheckBox"))
            {
                QCheckBox *checkBox = static_cast<QCheckBox *>(cellWidget);
                                checkBox->setChecked(!checkBox->isChecked());
                                return true;


            }
        }
    }
    return QWidget::eventFilter(watched, event);
}
//初始化
void borrowerBookwidget::init()
{
    books.clear();
    currentbooks.clear();
    //隐藏未找到标签
    ui->notfound->hide();
    qDebug() << "zhongduan ";
    //读取文件
    QFile file("book.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QTextCodec* codec = QTextCodec::codecForName("UTF-8");  // 设置UTF-8编码解码器

            QTextStream in(&file);
            in.setCodec(codec);
            while (!in.atEnd()) {

                        QString line = in.readLine();
                       // qDebug() << "读取到的原始行数据: " << line;
                        QStringList parts = line.split("/");
                         //qDebug() << "分割后的各部分数据: " << parts;
                        if (parts.size() == 8) {
                            Book book;
                            book.setName(parts[0]);//书名
                            book.setPublisher(parts[1]);//出版社
                            book.setAuthor(parts[2]);//作者
                            book.setISBN(parts[3]);//ISBN
                            book.setCategory(parts[4]);//种类
                            book.setCollectionLocation(parts[5]);//楼层
                            //qDebug()<<parts[5];
                            bool ok;
                            int currentCollection = parts[6].toInt(&ok);
                            if (ok) {
                                qDebug() << currentCollection;
                                book.setCurrentCollection(currentCollection);
                            }
                            int allCollection = parts[7].toInt(&ok);
                            if (ok) {
                                qDebug() << allCollection;
                                book.setAllCollection(allCollection);
                            }


                            books.push_back(book);
                        }
                    }
                    file.close();

                } else {
                    qDebug() << "无法打开文件";
                }
    //将book呈现在页面上
    //设置表格宽度
    qDebug()<<books.size();
    //int columnCount = ui->tableWidget->columnCount();
    //for (int i = 0; i < columnCount; ++i) {
        //ui->tableWidget->setColumnWidth(i, 100);
    //}
    // 设置表头
        QStringList headers;
        headers << "书名" << "出版社" << "作者" << "ISBN" << "类别"
                << "馆藏地点" << "当前数量" << "总馆藏数量"<<"选择";
        ui->tableWidget->setHorizontalHeaderLabels(headers);


        // 填充表格数据
        for (int row = 0; row < books.size(); ++row) {
            Book book = books[row];
            QTableWidgetItem* titleItem = new QTableWidgetItem(book.getName());
            QTableWidgetItem* publisherItem = new QTableWidgetItem(book.getPublisher());
            QTableWidgetItem* authorItem = new QTableWidgetItem(book.getAuthor());
            QTableWidgetItem* isbnItem = new QTableWidgetItem(book.getISBN());
            QTableWidgetItem* categoryItem = new QTableWidgetItem(book.getCategory());
            QTableWidgetItem* locationItem = new QTableWidgetItem(book.getCollectionLocation());
            QTableWidgetItem* currentCountItem = new QTableWidgetItem(QString::number(book.getCurrentCollection()));
            QTableWidgetItem* allCountItem = new QTableWidgetItem(QString::number(book.getAllCollection()));

            ui->tableWidget->setItem(row, 0, titleItem);
            ui->tableWidget->setItem(row, 1, publisherItem);
            ui->tableWidget->setItem(row, 2, authorItem);
            ui->tableWidget->setItem(row, 3, isbnItem);
            ui->tableWidget->setItem(row, 4, categoryItem);
            ui->tableWidget->setItem(row, 5, locationItem);
            ui->tableWidget->setItem(row, 6, currentCountItem);
            ui->tableWidget->setItem(row, 7, allCountItem);
            QCheckBox *checkBox = new QCheckBox(ui->tableWidget);
                ui->tableWidget->setCellWidget(row, 8, checkBox);  // 将选择框放置在第0行第1列（索引从0开始

            //qDebug() << "jjjj";
        }
        //显示数量
         ui->number_label->setText("共有"+ QString::number(books.size())+"条数据");
        //当前页面图书储存

        //复制

        currentbooks.clear();
        for (int i=0;i<books.size();i++) {
            qDebug()<<books[0].getName();
            Book book(books[i].getName(),books[i].getPublisher(),books[i].getAuthor(),books[i].getISBN(),
                      books[i].getCategory(),books[i].getCollectionLocation(),books[i].getCurrentCollection(),
                      books[i].getAllCollection());
            currentbooks.push_back(book);
        }
        //添加搜索提示
        QStringList wordList;
        for (const Book& book : books) {

                wordList << book.getName();
                qDebug()<<wordList;
            }


            // 创建QCompleter对象，并关联字符串列表
            QCompleter *completer = new QCompleter(wordList, this);
            // 设置匹配模式，Qt::MatchContains表示只要输入的内容包含在提示字符串中就匹配，可根据需求调整
            completer->setCompletionMode(QCompleter::PopupCompletion);
            completer->setCaseSensitivity(Qt::CaseInsensitive);

            // 将QCompleter对象关联到QLineEdit上
            ui->lineEdit->setCompleter(completer);







}


borrowerBookwidget::~borrowerBookwidget()
{
    delete ui;
}
void borrowerBookwidget::showallbook()
{

    ui->notfound->hide();
    ui->tableWidget->setRowCount(25);
    ui->tableWidget->setColumnCount(9);
    ui->notfound->hide();
    //设置表格宽度
    qDebug()<<books.size();
    //int columnCount = ui->tableWidget->columnCount();
    /*
    for (int i = 0; i < columnCount; ++i) {
        ui->tableWidget->setColumnWidth(i, 100);
    }
    */
    // 设置表头
        QStringList headers;
        headers << "书名" << "出版社" << "作者" << "ISBN" << "类别"
                << "馆藏地点" << "当前数量" << "总馆藏数量"<<"选择";
        ui->tableWidget->setHorizontalHeaderLabels(headers);
        // 临时存储搜索结果的容器
            std::vector<Book> allbook;
            for (int row = 0; row < books.size(); ++row) {
                Book book = books[row];
                //勾选已有
                if(ui->radioButton->isChecked())
                {
                    if(book.getCurrentCollection()==0)
                    {
                        continue;
                    }
                }
                allbook.push_back(book);
            }



        // 填充表格数据
        for (int row = 0; row < allbook.size(); ++row) {
            Book book = allbook[row];
            //勾选已有
            if(ui->radioButton->isChecked())
            {
                if(book.getCurrentCollection()==0)
                {
                    continue;
                }
            }
            QTableWidgetItem* titleItem = new QTableWidgetItem(book.getName());
            QTableWidgetItem* publisherItem = new QTableWidgetItem(book.getPublisher());
            QTableWidgetItem* authorItem = new QTableWidgetItem(book.getAuthor());
            QTableWidgetItem* isbnItem = new QTableWidgetItem(book.getISBN());
            QTableWidgetItem* categoryItem = new QTableWidgetItem(book.getCategory());
            QTableWidgetItem* locationItem = new QTableWidgetItem(book.getCollectionLocation());
            QTableWidgetItem* currentCountItem = new QTableWidgetItem(QString::number(book.getCurrentCollection()));
            QTableWidgetItem* allCountItem = new QTableWidgetItem(QString::number(book.getAllCollection()));

            ui->tableWidget->setItem(row, 0, titleItem);
            ui->tableWidget->setItem(row, 1, publisherItem);
            ui->tableWidget->setItem(row, 2, authorItem);
            ui->tableWidget->setItem(row, 3, isbnItem);
            ui->tableWidget->setItem(row, 4, categoryItem);
            ui->tableWidget->setItem(row, 5, locationItem);
            ui->tableWidget->setItem(row, 6, currentCountItem);
            ui->tableWidget->setItem(row, 7, allCountItem);
            QCheckBox *checkBox = new QCheckBox(ui->tableWidget);
                ui->tableWidget->setCellWidget(row, 8, checkBox);

            //qDebug() << "jjjj";
        }
        //显示数量
         ui->number_label->setText("共有"+ QString::number(allbook.size())+"条数据");
        currentbooks.clear();
        currentbooks=allbook;
        /*
        for (int i=0;i<books.size();i++) {
            qDebug()<<books[0].getName();
            Book book(books[i].getName(),books[i].getPublisher(),books[i].getAuthor(),books[i].getISBN(),
                      books[i].getCategory(),books[i].getCollectionLocation(),books[i].getCurrentCollection(),
                      books[i].getAllCollection());
            currentbooks.push_back(book);
        }
        */


        qDebug()<<"showall函数调用";
}
void borrowerBookwidget::Currentbooks()
{
    if(currentbooks.empty())
    {
       ui->notfound->show();
       return;
    }

    ui->tableWidget->setRowCount(100);
    ui->tableWidget->setColumnCount(9);
    //ui->notfound->hide();
    //设置表格宽度
    qDebug()<<books.size();
    int columnCount = ui->tableWidget->columnCount();
    for (int i = 0; i < columnCount; ++i) {
        ui->tableWidget->setColumnWidth(i, 100);
    }
    // 设置表头
        QStringList headers;
        headers << "书名" << "出版社" << "作者" << "ISBN" << "类别"
                << "馆藏地点" << "当前数量" << "总馆藏数量"<<"选择";
        ui->tableWidget->setHorizontalHeaderLabels(headers);


        // 填充表格数据
        for (int row = 0; row < currentbooks.size(); ++row) {
            Book book = currentbooks[row];
            QTableWidgetItem* titleItem = new QTableWidgetItem(book.getName());
            QTableWidgetItem* publisherItem = new QTableWidgetItem(book.getPublisher());
            QTableWidgetItem* authorItem = new QTableWidgetItem(book.getAuthor());
            QTableWidgetItem* isbnItem = new QTableWidgetItem(book.getISBN());
            QTableWidgetItem* categoryItem = new QTableWidgetItem(book.getCategory());
            QTableWidgetItem* locationItem = new QTableWidgetItem(book.getCollectionLocation());
            QTableWidgetItem* currentCountItem = new QTableWidgetItem(QString::number(book.getCurrentCollection()));
            QTableWidgetItem* allCountItem = new QTableWidgetItem(QString::number(book.getAllCollection()));

            ui->tableWidget->setItem(row, 0, titleItem);
            ui->tableWidget->setItem(row, 1, publisherItem);
            ui->tableWidget->setItem(row, 2, authorItem);
            ui->tableWidget->setItem(row, 3, isbnItem);
            ui->tableWidget->setItem(row, 4, categoryItem);
            ui->tableWidget->setItem(row, 5, locationItem);
            ui->tableWidget->setItem(row, 6, currentCountItem);
            ui->tableWidget->setItem(row, 7, allCountItem);
            QCheckBox *checkBox = new QCheckBox(ui->tableWidget);
                ui->tableWidget->setCellWidget(row, 8, checkBox);

            //qDebug() << "jjjj";
        }
        //显示数量
         ui->number_label->setText("共有"+ QString::number(currentbooks.size())+"条数据");



        qDebug()<<"showall函数调用";
}
void borrowerBookwidget::on_lineEdit_editingFinished()
{

          qDebug()<<"搜索";
          QString searchText = ui->lineEdit->text();  // 获取搜索框中的文本（假设界面有个QLineEdit用于输入搜索内容
             if (searchText.isEmpty())
             {
                 qDebug()<<"搜索为空";
                 ui->comboBox->setCurrentIndex(0);
                 showallbook();
                 return;  // 如果搜索框为空，直接返回，不进行搜索操作
             }
             // 临时存储搜索结果的容器
                 std::vector<Book> searchResults;
                 for (const auto& book : books)
                 {
                     if (book.getName().contains(searchText, Qt::CaseInsensitive) ||
                         book.getAuthor().contains(searchText, Qt::CaseInsensitive) ||
                         book.getPublisher().contains(searchText, Qt::CaseInsensitive)){
                         searchResults.push_back(book);
                         qDebug()<<"符合";
                     }

                 }
                 if(searchResults.empty())
                 {
                    // ui->tableWidget->setRowCount(0);
                     //ui->tableWidget->setColumnCount(9);
                     //显示没有找到相关书籍
                     //ui->tableWidget->clear();
                     qDebug()<<"不符合111111111";
                     ui->notfound->show();
                     currentbooks.clear();
                     //return;
                 }

   qDebug()<<" clear"<<searchResults.size();
   // 清空表格原有内容
       ui->tableWidget->setRowCount(0);
       ui->tableWidget->setColumnCount(9);


       // 设置表头
       //设置行列
       //ui->tableWidget->setRowCount(100);
       //ui->tableWidget->setColumnCount(8);
       QStringList headers;
       headers << "书名" << "出版社" << "作者" << "ISBN" << "类别"
               << "馆藏地点" << "当前数量" << "总馆藏数量"<<"选择";
       ui->tableWidget->setHorizontalHeaderLabels(headers);
       //分类调整至默认
       ui->comboBox->setCurrentIndex(0);


       // 填充搜索结果到表格
       for (size_t row = 0; row < searchResults.size(); ++row)
       {
           Book book = searchResults[row];
           QTableWidgetItem* titleItem = new QTableWidgetItem(book.getName());
           QTableWidgetItem* publisherItem = new QTableWidgetItem(book.getPublisher());
           QTableWidgetItem* authorItem = new QTableWidgetItem(book.getAuthor());
           QTableWidgetItem* isbnItem = new QTableWidgetItem(book.getISBN());
           QTableWidgetItem* categoryItem = new QTableWidgetItem(book.getCategory());
           QTableWidgetItem* locationItem = new QTableWidgetItem(book.getCollectionLocation());
           QTableWidgetItem* currentCountItem = new QTableWidgetItem(QString::number(book.getCurrentCollection()));
           QTableWidgetItem* allCountItem = new QTableWidgetItem(QString::number(book.getAllCollection()));

           ui->tableWidget->setRowCount(row + 1);
           ui->tableWidget->setItem(row, 0, titleItem);
           ui->tableWidget->setItem(row, 1, publisherItem);
           ui->tableWidget->setItem(row, 2, authorItem);
           ui->tableWidget->setItem(row, 3, isbnItem);
           ui->tableWidget->setItem(row, 4, categoryItem);
           ui->tableWidget->setItem(row, 5, locationItem);
           ui->tableWidget->setItem(row, 6, currentCountItem);
           ui->tableWidget->setItem(row, 7, allCountItem);
           QCheckBox *checkBox = new QCheckBox(ui->tableWidget);
               ui->tableWidget->setCellWidget(row, 8, checkBox);

       }
       //显示数量
        ui->number_label->setText("共有"+ QString::number(searchResults.size())+"条数据");


       currentbooks.clear();
       for (int i=0;i<searchResults.size();i++) {
           qDebug()<<searchResults[0].getName();
           Book book(searchResults[i].getName(),searchResults[i].getPublisher(),searchResults[i].getAuthor(),searchResults[i].getISBN(),
                     searchResults[i].getCategory(),searchResults[i].getCollectionLocation(),searchResults[i].getCurrentCollection(),
                     searchResults[i].getAllCollection());
           currentbooks.push_back(book);
       }


       qDebug()<<"search函数调用"<<currentbooks.size();


}

void borrowerBookwidget::on_comboBox_currentIndexChanged(int index)
{
    QString selectedCategory = ui->comboBox->itemText(index);  // 获取当前选中的分类文本
        if (selectedCategory.isEmpty()) {
            return;  // 如果获取到的分类文本为空，直接返回，不进行后续操作
        }

        // 临时存储符合分类条件的图书信息的容器
        std::vector<Book> categoryBooks;
        //默认显示所有
        if(index==0)
        {

            showallbook();
            return;
        }
        for (const auto& book : books) {
            if (book.getCategory() == " "+selectedCategory+" ") {
                //如果勾选了已有
                if(ui->radioButton->isChecked())
                {

                    if(book.getCurrentCollection()==0)
                    {
                        continue;
                    }
                    //categoryBooks.push_back(book);
                }
                categoryBooks.push_back(book);
            }
        }
        if(categoryBooks.empty())
        {

            //未找到图书
            ui->notfound->show();
        }
        //找到了
        ui->notfound->hide();


        // 清空表格原有内容
        ui->tableWidget->setRowCount(0);
        ui->tableWidget->setColumnCount(9);

        // 设置表头
        /*
        int columnCount = ui->tableWidget->columnCount();
        for (int i = 0; i < columnCount; ++i) {
            ui->tableWidget->setColumnWidth(i, 100);
        }
        */
        QStringList headers;
        headers << "书名" << "出版社" << "作者" << "ISBN" << "类别"
                << "馆藏地点" << "当前数量" << "总馆藏数量"<<"选择";
        ui->tableWidget->setHorizontalHeaderLabels(headers);

        // 填充符合分类条件的图书信息到表格
        for (size_t row = 0; row < categoryBooks.size(); ++row) {
            Book book = categoryBooks[row];
            QTableWidgetItem* titleItem = new QTableWidgetItem(book.getName());
            QTableWidgetItem* publisherItem = new QTableWidgetItem(book.getPublisher());
            QTableWidgetItem* authorItem = new QTableWidgetItem(book.getAuthor());
            QTableWidgetItem* isbnItem = new QTableWidgetItem(book.getISBN());
            QTableWidgetItem* categoryItem = new QTableWidgetItem(book.getCategory());
            QTableWidgetItem* locationItem = new QTableWidgetItem(book.getCollectionLocation());
            QTableWidgetItem* currentCountItem = new QTableWidgetItem(QString::number(book.getCurrentCollection()));
            QTableWidgetItem* allCountItem = new QTableWidgetItem(QString::number(book.getAllCollection()));

            ui->tableWidget->setRowCount(row + 1);
            ui->tableWidget->setItem(row, 0, titleItem);
            ui->tableWidget->setItem(row, 1, publisherItem);
            ui->tableWidget->setItem(row, 2, authorItem);
            ui->tableWidget->setItem(row, 3, isbnItem);
            ui->tableWidget->setItem(row, 4, categoryItem);
            ui->tableWidget->setItem(row, 5, locationItem);
            ui->tableWidget->setItem(row, 6, currentCountItem);
            ui->tableWidget->setItem(row, 7, allCountItem);
            QCheckBox *checkBox = new QCheckBox(ui->tableWidget);
                ui->tableWidget->setCellWidget(row, 8, checkBox);

        }
        //显示数量
         ui->number_label->setText("共有"+ QString::number(categoryBooks.size())+"条数据");
        ui->lineEdit->clear();
        currentbooks=categoryBooks;

        qDebug()<<"fenlei函数调用";

}

//点击显示当前可借

void borrowerBookwidget::on_radioButton_clicked(bool checked)
{
    //取消已有
    if(!checked)
    {
        //搜索框清零
        ui->lineEdit->clear();
        int i=ui->comboBox->currentIndex();
        on_comboBox_currentIndexChanged(i);
     return;
    }
    // 临时存储符合分类条件的图书信息的容器
    std::vector<Book> currentcollection;

    for (const auto& book : currentbooks) {
        if (book.getCurrentCollection()>0) {
            currentcollection.push_back(book);
        }
    }
    // 清空表格原有内容
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(9);
    if(currentcollection.size()==0)
    {
        ui->notfound->show();
       return;
    }
    // 设置表头
    QStringList headers;
    headers << "书名" << "出版社" << "作者" << "ISBN" << "类别"
            << "馆藏地点" << "当前数量" << "总馆藏数量"<<"选择";
    ui->tableWidget->setHorizontalHeaderLabels(headers);

    // 填充符合分类条件的图书信息到表格
    for (size_t row = 0; row < currentcollection.size(); ++row) {
        Book book = currentcollection[row];
        QTableWidgetItem* titleItem = new QTableWidgetItem(book.getName());
        QTableWidgetItem* publisherItem = new QTableWidgetItem(book.getPublisher());
        QTableWidgetItem* authorItem = new QTableWidgetItem(book.getAuthor());
        QTableWidgetItem* isbnItem = new QTableWidgetItem(book.getISBN());
        QTableWidgetItem* categoryItem = new QTableWidgetItem(book.getCategory());
        QTableWidgetItem* locationItem = new QTableWidgetItem(book.getCollectionLocation());
        QTableWidgetItem* currentCountItem = new QTableWidgetItem(QString::number(book.getCurrentCollection()));
        QTableWidgetItem* allCountItem = new QTableWidgetItem(QString::number(book.getAllCollection()));

        ui->tableWidget->setRowCount(row + 1);
        ui->tableWidget->setItem(row, 0, titleItem);
        ui->tableWidget->setItem(row, 1, publisherItem);
        ui->tableWidget->setItem(row, 2, authorItem);
        ui->tableWidget->setItem(row, 3, isbnItem);
        ui->tableWidget->setItem(row, 4, categoryItem);
        ui->tableWidget->setItem(row, 5, locationItem);
        ui->tableWidget->setItem(row, 6, currentCountItem);
        ui->tableWidget->setItem(row, 7, allCountItem);
        QCheckBox *checkBox = new QCheckBox(ui->tableWidget);
            ui->tableWidget->setCellWidget(row, 8, checkBox);

    }
    //显示数量
     ui->number_label->setText("共有"+ QString::number(currentcollection.size())+"条数据");




}


void borrowerBookwidget::on_borrow_button_clicked()
{
    //没有选择
    bool hasChecked = false;  // 用于标记是否有书籍被勾选
       for (int row = 0; row < ui->tableWidget->rowCount(); row++)
       {
           QWidget *cellWidget = ui->tableWidget->cellWidget(row, ui->tableWidget->columnCount() - 1);
           if (cellWidget && cellWidget->inherits("QCheckBox"))
           {
               QCheckBox *checkBox = static_cast<QCheckBox *>(cellWidget);
               if (checkBox->isChecked())
               {
                   hasChecked = true;
                   break;  // 只要发现有一个被勾选的，就标记并跳出循环，无需继续检查后面的行
               }
           }
       }

       if (!hasChecked)
       {
           QMessageBox::warning(this, "提示", "还未选择");
           return;  // 直接返回，不执行后续的文件操作等逻辑
       }


       //选择了

    QFile borrowFile("borrowbooks.txt");
       if (borrowFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
       {
           QTextStream out(&borrowFile);
           QTextCodec *codec = QTextCodec::codecForName("UTF-8");
            out.setCodec(codec);
           for (int row = 0; row < ui->tableWidget->rowCount(); row++)
           {
               QWidget *cellWidget = ui->tableWidget->cellWidget(row, ui->tableWidget->columnCount() - 1);
               if (cellWidget && cellWidget->inherits("QCheckBox"))
               {
                   QCheckBox *checkBox = static_cast<QCheckBox *>(cellWidget);
                   if (checkBox->isChecked())
                   {
                       Book book = currentbooks[row];  // 假设books容器存储了所有书籍信息，根据实际情况调整获取方式
                       // 将书籍信息写入borrowbooks.txt文件，格式可根据需求自定义，这里以简单的每行一条信息示例
                       // 获取当前日期并格式化为 yyyy-MM-dd 形式
                         QDateTime currentDateTime = QDateTime::currentDateTime();
                         QString currentDate = currentDateTime.toString("yyyy-MM-dd");
                      // 将书籍信息以及当前日期写入borrowbooks.txt文件，格式可根据需求自定义，这里添加日期信息
                          out << book.getName() << "/" << book.getPublisher() << "/" << book.getAuthor()
                          << "/" << book.getISBN() << "/" << book.getCategory() << "/"
                          << book.getCollectionLocation() << "/" << book.getCurrentCollection()
                          << "/" << book.getAllCollection() << "/" << currentDate << "\n";

                       // 更新book.txt文件中对应书籍的数量（这里假设book.txt文件格式和之前读取时一致）
                       QFile bookFile("book.txt");
                       if (bookFile.open(QIODevice::ReadOnly | QIODevice::Text))
                       {
                           QTextStream in(&bookFile);
                           in.setCodec(codec);
                           QStringList allLines;
                           while (!in.atEnd())
                           {
                               QString line = in.readLine();
                               QStringList parts = line.split("/");
                               if (parts.size() == 8 && parts[3] == book.getISBN())
                               {
                                   bool ok;
                                   int currentCount = parts[6].toInt(&ok);
                                   if (ok && currentCount > 0)
                                   {
                                       parts[6] = QString::number(currentCount - 1);
                                       line = parts.join("/");
                                   }
                               }
                               allLines.append(line);
                           }
                           bookFile.close();

                           //写文本进行更新
                           if (bookFile.open(QIODevice::WriteOnly | QIODevice::Text))
                           {
                               QTextStream updateOut(&bookFile);
                               updateOut.setCodec(codec);
                               for (const QString &line : allLines)
                               {
                                   updateOut << line << "\n";
                               }
                               bookFile.close();
                           }
                       }
                   }
               }
           }
           borrowFile.close();
           //弹出提示显示借阅图书信息
           qDebug()<<"文件打开成功";

           QMessageBox::information(this, "提示", "借阅信息已成功写入文件，对应书籍数量已更新。");  // 弹出提示信息，可选

           //弹出界面显示借阅书籍的信息
           //弹出提示框








           init();
             qDebug()<<"文件打开成功";

       }
       else
       {
           //QMessageBox::warning(this, "错误", "无法打开borrowbooks.txt文件进行写入操作。");  // 弹出错误提示信息，可选
           qDebug()<<"文件打开失败";
       }
       //重新初始化界面


}












void borrowerBookwidget::on_cancelallbutton_clicked()
{
    for (int row = 0; row < ui->tableWidget->rowCount(); row++)
      {
          QWidget *cellWidget = ui->tableWidget->cellWidget(row, ui->tableWidget->columnCount() - 1);
          if (cellWidget && cellWidget->inherits("QCheckBox"))
          {
              QCheckBox *checkBox = static_cast<QCheckBox *>(cellWidget);
              if (checkBox->isChecked())
              {
                  checkBox->setChecked(false);
              }
          }
      }
}
